require("../util.js");
require("../../jsMt/util.js");
require("../component/components.js");
require("../component/indexList.js");
var vm = new Vue({
    el:"#mediaHome",
    data:{
        indexs:1,
        id:"", //id
        loginUid:"",  //用户id
        userInfo:{},//企业信息
        data:{
            uid:"",  //id
            pageNumber:"1",//页数
            sortId:"rank", // 排序字段 默认排序 rank 最多浏览 play_count 最新上传 create_date
            sortValue:"asc" // 排序值 asc 升 desc 降
        },
        userAdvList:{
            list:{}
        },//视频
        userAdvList1:[],
        userAdvList2:[],
        allAlbumByUserId:{
            list:{}
        },//相册
        allAlbumByUserId1:[],
        allAlbumByUserId2:[],
        allProductByUserId:{
            list:{}
        },//产品
        allProductByUserId1:[],
        allProductByUserId2:[],
        loadFlag:false,
        followState:""//关注状态
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.loginUid=this.searchObject&&this.searchObject.loginUid;
        this.data.uid=this.searchObject&&this.searchObject.uid;
        this.getUserInfo();
        this.getUserAdvList();
        this.getAllAlbumByUserId();
        this.getAllProductByUserId();
        lazyload();
    },
    methods:{
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*tab切换*/
        tabSwitch:function(index){
            this.indexs=index;
            if(this.indexs==1){
                return false;
            }else{
                this.reset();
            }
        },
        //筛选重置
        reset:function(){
            this.data.pageNumber="1";
            this.data.sortId="rank";
            this.data.sortValue="asc";
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //通过用户id获取企业信息
        getUserInfo:function(){
            var that=this;
            var data={
                uid:that.data.uid,
                loginUid:that.loginUid
            };
            this.ajax({url:"/getUserInfo",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.userInfo=b.json.data;
                    that.id=b.json.data.id;
                    that.followState=b.json.data.followState;
                }else{
                    vm.$refs.prompt.toast("获取信息失败！")
                }
            });
        },
        //通过用户id 关注
        getFollowAdd:function(){
            var that=this;
            var data={
                followUserId:that.id,
                loginUid:that.loginUid
            };
            this.ajax({url:"/getFollowAdd",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.followState=1;
                }else{
                    vm.$refs.prompt.toast("关注失败！")
                }
            });
        },
        //通过用户id 关注id 取消关注
        getFollowDelete:function(){
            var that=this;
            var data={
                followUserId:that.id,
                loginId:that.loginUid
            };
            this.ajax({url:"/getFollowDelete",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.followState=0;
                }else{
                    vm.$refs.prompt.toast("取消关注失败！")
                }
            });
        },
        //tab排序
        tabSort:function(num){
            switch(this.indexs){
                case 2:
                    this.getJudge(num,"play_count");
                    this.getUserAdvList();
                    break;
                case 3:
                    this.getJudge(num,"view_count");
                    this.getAllAlbumByUserId();
                    break;
                case 4:
                    this.getJudge(num,"view_count");
                    this.getAllProductByUserId();
                    break;
            }
            console.log(this.data)
        },
        //判断
        getJudge:function(num,sortId){
            if(num==0){
                if(this.data.sortId=="rank"){
                    if(this.data.sortValue=="asc"){
                        this.data.sortValue="desc";
                    }else{
                        this.data.sortValue="asc";
                    }
                }else{
                    this.data.sortId="rank";
                    this.data.sortValue="asc"
                }
            }else if(num==1){
                if(this.data.sortId==sortId){
                    if(this.data.sortValue=="asc"){
                        this.data.sortValue="desc";
                    }else{
                        this.data.sortValue="asc";
                    }
                }else{
                    this.data.sortId=sortId;
                    this.data.sortValue="asc"
                }
            }else if(num==2){
                if(this.data.sortId=="create_date"){
                    if(this.data.sortValue=="asc"){
                        this.data.sortValue="desc";
                    }else{
                        this.data.sortValue="asc";
                    }
                }else{
                    this.data.sortId="create_date";
                    this.data.sortValue="asc"
                }
            }
        },
        //通过用户id 获取视频列表
        getUserAdvList:function(){
            var that=this;
            var data={
                uid:that.data.uid,
                pageNumber:that.data.pageNumber,
                sortId:that.data.sortId,
                sortValue:that.data.sortValue
            };
            this.ajax({url:"/getUserAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.userAdvList=b.json.data;
                    that.userAdvList1=that.userAdvList.list;
                    if(that.userAdvList.pageNumber>1){
                        if(that.userAdvList.list.length>0) {
                            that.userAdvList2 = that.userAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.userAdvList2= that.userAdvList1;
                    }
                    //console.log(that.hotAdvList);
                    vm.loadFlag=false;
                    lazyload();
                    //console.log(that.userAdvList2);
                }else{
                    that.userAdvList={};
                    that.userAdvList1=[];
                    that.userAdvList2=[];
                    vm.$refs.prompt.toast("获取视频信息失败！")
                }
            });
        },
        //通过用户id 获取相册列表
        getAllAlbumByUserId:function(){
            var that=this;
            var data={
                uid:that.data.uid,
                pageNumber:that.data.pageNumber,
                sortId:that.data.sortId,
                sortValue:that.data.sortValue
            };
            this.ajax({url:"/getAllAlbumByUserId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.allAlbumByUserId=b.json.data;
                    that.allAlbumByUserId1=that.allAlbumByUserId.list;
                    if(that.allAlbumByUserId.pageNumber>1){
                        if(that.allAlbumByUserId.list.length>0) {
                            that.allAlbumByUserId2 = that.allAlbumByUserId2.concat(b.json.data.list);
                        }
                    }else{
                        that.allAlbumByUserId2= that.allAlbumByUserId1;
                    }
                    //console.log(that.hotAdvList);
                    vm.loadFlag=false;
                    lazyload();
                    //console.log(that.allAlbumByUserId2);
                }else{
                    that.allAlbumByUserId={};
                    that.allAlbumByUserId1=[];
                    that.allAlbumByUserId2=[];
                    vm.$refs.prompt.toast("获取相册失败！")
                }
            });
        },
        //通过用户id 获取产品列表
        getAllProductByUserId:function(){
            var that=this;
            var data={
                uid:that.data.uid,
                pageNumber:that.data.pageNumber,
                sortId:that.data.sortId,
                sortValue:that.data.sortValue
            };
            this.ajax({url:"/getAllProductByUserId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.allProductByUserId=b.json.data;
                    that.allProductByUserId1=that.allProductByUserId.list;
                    if(that.allProductByUserId.pageNumber>1){
                        if(that.allProductByUserId.list.length>0) {
                            that.allProductByUserId2 = that.allProductByUserId2.concat(b.json.data.list);
                        }
                    }else{
                        that.allProductByUserId2= that.allProductByUserId1;
                    }
                    //console.log(that.hotAdvList);
                    vm.loadFlag=false;
                    lazyload();
                    //console.log(that.allProductByUserId2);
                }else{
                    that.allProductByUserId={};
                    that.allProductByUserId1=[];
                    that.allProductByUserId2=[];
                    vm.$refs.prompt.toast("获取产品失败！")
                }
            });
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag;
    if(vm.indexs==2){
        loadFlag = vm.userAdvList.lastPage;
    }else if(vm.indexs==3){
        loadFlag = vm.allAlbumByUserId.lastPage;
    }else if(vm.indexs==4){
        loadFlag = vm.allProductByUserId.lastPage;
    }
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.data.pageNumber++;
            if(vm.indexs==2){
                vm.getUserAdvList();
            }else if(vm.indexs==3){
                vm.getAllAlbumByUserId();
            }else if(vm.indexs==4){
                vm.getAllProductByUserId();
            }

        }else{
            lazyload();
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function lazyload(){
    $("img.lazy").lazyload();
}